package com.banciyuan.bcywebview.biz.login;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.myinterface.PushActionInterface;
import com.banciyuan.bcywebview.biz.apprecommend.RecommendAcgActivity;
import com.banciyuan.bcywebview.utils.http.CommonApiUtils;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginHelper {

    private Context mContext;
    private RequestQueue mQueue;

    private PushActionInterface mLoginInterface;

    public LoginHelper(LoginActivity mContext, PushActionInterface mLoginInterface) {
        this.mContext = mContext;
        this.mLoginInterface = mLoginInterface;
        mQueue = VolleyQueue.getRquest(mContext);
    }

    public void doLogin(String name, String pwd) {
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
            mLoginInterface.onNetError(mContext.getResources().getString(R.string.login_error_null));
            return;
        }

        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext, mLoginInterface))
                    dealLoginJson(response, mContext);
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mLoginInterface.onNetError(mContext.getResources().getString(R.string.network_abnormal));
            }
        };
        if (!CommonApiUtils.doLoginAction(mContext, mQueue, onSuccess, onError, name, pwd)) {
            mLoginInterface.onNetError("");
        }
    }

    private void dealLoginJson(String result, Context mContext) {
        try {
            JSONObject response = new JSONObject(result);
            UserDataHelper.Login(response.getJSONObject(HttpUtils.UPLOAD_DATA), mContext);
            if (isShowRecommend()) {
                gotoUtil.replaceMainClearTop(mContext);
            } else {
                gotoUtil.replaceAct(mContext, RecommendAcgActivity.class);
            }
        } catch (JSONException e) {
            mLoginInterface.onNetError(mContext.getResources().getString(R.string.network_abnormal));
        }
    }

    private boolean isShowRecommend() {
        boolean showrecommend = SPHelper.getBoolean(mContext, SPConstant.BCY_SHOWRECOMMEND, false);
        return showrecommend;
    }
}